import { CommodityUiAction } from '../uiAction/commodityUiAction'
import { DataPage } from '../../network/communicationConverter'
import { MetalUiAction } from "../uiAction/metalUiAction";
import { Engine } from "../engine";
import { SubCommodityFilter, SubCommodityMetalFilterTypeEnum } from '../commodity/subCommodity'
import { TargetObjectFilter,TargetObjectPlatformType } from '../targetOrg/targetObject'

export class MetalStatics {
    customers = [];
    metalCommodities = [];  // 商户支持的贵金属商品 (贵金属本身) SubCommodity

    _commodityUiAction = new CommodityUiAction(); // CommodityUiAction
    _metalUiAction = new MetalUiAction();   // MetalUiAction
    _filter = new TargetObjectFilter({objPlatformType: TargetObjectPlatformType.TUserCreated});

        _errorStatus = false;

    async initData() {
        await Promise.all([
            this.getMetalCommodityInfo(),
            this.getCustomers(),
        ]);
    }

    // 获取贵金属商品信息
    async getMetalCommodityInfo() {
        const res = await this._commodityUiAction.getCommoditySpecList(
            new DataPage(1, 0, 100),  // 大一些保障所有贵金属拿到     
            (new SubCommodityFilter({ metalType: SubCommodityMetalFilterTypeEnum.MetalSelfOnly })).toJson()
        )

        if (res?.data?.dataList) {
            this.metalCommodities = res.data.dataList;
            return null;
        }

        this._errorStatus = true;
        return "get metal commodity info failed"
    }

    // 获取客户列表
    async getCustomers(pageNum = 1, pageSize = 1000,) {
        let ret = null;
        try {
            const res = await this._metalUiAction.getCustomersMetalBalanceData({
                pageInfo: new DataPage(pageNum, 0, pageSize),
                filter: this._filter.toJson()
            });
            if (res?.data?.dataList) {
                ret = {
                    dataList: res.data.dataList,
                    total: res.data.pageInfo?.total || res.data.dataList.length
                };

                this.customers = res.data.dataList;
            }
            else if (res?.resErr) {
                this._errorStatus = true;
                console.error('获取综客户列表失败: ' + res.resErr)
            }

           

        }
        catch (e) {
            this._errorStatus = true;
          
        }
        finally {
            return ret;
        }
    }
}